package cf.luobo.covid_19.dao;

import cf.luobo.covid_19.javaBean.pojo.ErrMsg;
import cf.luobo.covid_19.javaBean.pojo.Users;
import cf.luobo.covid_19.utils.MyJDBC;

import java.sql.*;

public class UsersDao {
    private Connection connection;
    private Statement statement;                    //静态SQL操作
    private PreparedStatement preparedStatement;    //动态SQL操作

    //根据ID查询
    public Users getUsers(int id) {
        Users users = null;
        String sql = "select * from users where id = ?";
        ResultSet resultSet = null;
        try {
            connection = MyJDBC.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                int ids = resultSet.getInt("id");
                String uname = resultSet.getString("uname");
                String pwd = resultSet.getString("pwd");
                int age = resultSet.getInt("age");
                String sex = resultSet.getString("sex");
                boolean admin = resultSet.getBoolean("admin");
                users = new Users(id, uname, pwd, age, sex, admin);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyJDBC.close(resultSet, preparedStatement, connection);
        }
        return users;
    }

    //修改
    public ErrMsg updateUser(Users users) {
        ErrMsg err = new ErrMsg(404, "Unknown Error");
        String sql = "UPDATE users SET uname = ?, pwd = ?, age = ?, sex = ?, admin = ? WHERE id = ?";
        try {
            connection = MyJDBC.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, users.getUname());
            preparedStatement.setString(2, users.getPwd());
            preparedStatement.setInt(3, users.getAge());
            preparedStatement.setString(4, users.getSex());
            preparedStatement.setBoolean(5, users.getAdmin());
            preparedStatement.setInt(6, users.getId());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("有" + i + "条数据修改成功...");
                err.setErrcode(0);
                err.setErrmsg("OK");
            } else {
                err.setErrcode(1);
                err.setErrmsg("更新失败，无此账号，请使用用户录入");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MyJDBC.close(preparedStatement, connection);
        }
        return err;
    }

    //添加用户
    public ErrMsg saveUsers(Users users) {
        ErrMsg err = new ErrMsg(404, "Unknown Error");
        String sql = "INSERT INTO users (id,uname,pwd,age,sex,admin) VALUES (?,?,?,?,?,?)";
        try {
            connection = MyJDBC.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, users.getId());
            preparedStatement.setString(2, users.getUname());
            preparedStatement.setString(3, users.getPwd());
            preparedStatement.setInt(4, users.getAge());
            preparedStatement.setString(5, users.getSex());
            preparedStatement.setBoolean(6, users.getAdmin());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("有" + i + "条数据添加成功...");
                err.setErrcode(0);
                err.setErrmsg("OK");
            } else {
                err.setErrcode(1);
                err.setErrmsg("添加失败");
            }
        } catch (SQLException e) {
            String message = e.getMessage();
            if (message.contains("Duplicate entry")) {
                err.setErrmsg("账号已存在！");
                err.setErrcode(2);
            } else {
                e.printStackTrace();
                err.setErrmsg(e.getMessage());
            }
        } finally {
            MyJDBC.close(preparedStatement, connection);
        }
        return err;
    }
}
